// 超级管理员
const experss = require("express")
const {
    asyncHandler
} = require("../getSendResult")
const router = experss.Router()
const multer = require("multer");
const {
    upload
} = require("../../services/CurrServ")
const {
    addAdmin,
    getAdmin,
    resetting,
    getRoomRate,
    addRoomRate,
    addDormitory,
    allCleaning,
    addCleaning,
    delCleaning,
    allAdmin,
    quitDormitory,
    updateDAdmin,
    deladmin,
    quitFloorCleaning,
    delDormitory,
    cleaning,
    updateFCleaning,
    getCheckInRecord,
    getClass,
    getUnallocated,
    distributionRoom
} = require("../../services/superServ");
const init = require("../../util/init")
// 权限判定
const checkPermission = require("../checkPermission")


router.all("*", (req, res, next) => {
    checkPermission(req, res, next, [0])
})

/**
 * 创建管理员
 */
router.post("/addAdmin", multer().single("avatar"), asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        const reslut = upload(req.file, async function (filename) {
            const body = {
                adminId: req.body.adminId,
                loginPwd: req.body.loginPwd,
                phone: req.body.phone,
                Nickname: req.body.Nickname,
                authentication: +req.body.authentication,
                avatar: filename ? `/upload/${filename}` : `/upload/admin.webp`
            }

            const res = await addAdmin(body)
            return res;

        })
        return reslut;
    })
    return resluts

}))
/**
 * 获取已有的管理员信息
 */
router.get("/getAdmin", asyncHandler(async (req, res) => {
    const reslut = await getAdmin(+req.query.authentication || null)
    return reslut
}))
/**
 * 重置管理员密码
 */
router.put("/resetting", asyncHandler(async (req, res) => {

    const resluts = await init.sequelize.transaction(async t => {
        const reslut = await resetting({
            adminId: req.body.adminId,
            phone: req.body.phone,
            Nickname: req.body.nickname,
            newPwd: req.body.newPwd
        })
        return reslut
    })
    return resluts
}))
/**
 * 查询宿舍费
 */
router.get("/getRoomRate/:peopleNum", asyncHandler(async (req, res) => {
    const reslut = await getRoomRate(+req.params.peopleNum)
    return reslut
}))
/**
 * 创建宿舍楼
 */
router.post("/addDormitory", asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        let RoomRateId = +req.body.RoomRateId || null
        if (!+req.body.isExits) {
            RoomRateId = await addRoomRate(+req.body.fewPeople, +req.body.price)
        }
        const reslut = await addDormitory({
            DormitoryName: req.body.dormitoryName,
            floorNum: +req.body.floorNum,
            roomNum: +req.body.roomNum,
            fewPeople: +req.body.fewPeople,
            RoomRateId
        })
        return reslut
    })
    return resluts

}))
/**
 * 分配保洁人员
 */
router.put("/allCleaning", asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        const reslut = await allCleaning({
            floorId: +req.body.floorId,
            cleaningId: +req.body.cleaningId
        })
        return reslut
    })
    return resluts

}))
/**
 * 添加保洁人员
 */
router.post("/addCleaning", multer().single("avatar"), asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        const reslut = upload(req.file, async function (filename) {
            const body = {
                cleaningName: req.body.cleaningName,
                cleaningPhone: req.body.cleaningPhone,
                economize: req.body.economize,
                market: req.body.market,
                distinguish: req.body.distinguish,
                avatar: filename ? `/upload/${filename}` : `/upload/clen.png`
            }

            const res = await addCleaning(body)
            return res;

        })
        return reslut;
    })
    return resluts


}))
/**
 * 删除保洁人员
 */
router.delete("/delCleaning/:id", asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        const reslut = await delCleaning(+req.params.id)
        return reslut
    })
    return resluts

}))
/**
 * 分配管理员
 */
router.put("/allAdmin", asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        const reslut = await allAdmin({
            dormitoryId: +req.body.dormitoryId,
            adminInfoId: +req.body.adminInfoId
        })
        return reslut
    })
    return resluts
}))
/**
 * 退出宿舍楼管理员
 */
router.put("/quitDormitory", asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        const reslut = await quitDormitory(+req.body.dormitoryId)
        return reslut
    })
    return resluts

}))
/**
 * 修改宿舍管理员
 */
router.put("/updateDAdmin", asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        const reslut = await updateDAdmin({
            dormitoryId: +req.body.dormitoryId,
            oldAdminId: +req.body.oldAdminId,
            newAdminId: +req.body.newAdminId
        })
        return reslut
    })
    return resluts

}))

/**
 * 删除管理员
 */
router.delete("/deladmin/:id", asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        const reslut = await deladmin(+req.params.id)
        return reslut
    })
    return resluts

}))

/**
 * 退出楼层保洁
 */
router.put("/quitFloorCleaning", asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        const reslut = await quitFloorCleaning({
            floorId: +req.body.floorId,
            cleaningId: +req.body.cleaningId
        })
        return reslut
    })
    return resluts

}))
/**
 * 删除宿舍楼
 */
router.delete("/delDormitory/:id", asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        const reslut = await delDormitory(+req.params.id)
        return reslut
    })
    return resluts
}))
/**
 * 查询保洁人员
 */
router.get("/cleaning", asyncHandler(async (req, res) => {
    const cleaningObj = {
        page: +req.query.page || 1,
        limit: +req.query.limit || 10,
        cleaningNumber: req.query.cleaningNumber || null,
        cleaningName: req.query.cleaningName || null,
        isRegion: +req.query.isRegion || 0
    }
    const reslut = await cleaning(cleaningObj)
    return reslut
}))
/**
 * 修改楼层保洁人员
 */
router.put("/updateFCleaning", asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        const reslut = await updateFCleaning({
            floorId: +req.body.floorId,
            oldCleaningId: +req.body.oldCleaningId,
            newCleaningId: +req.body.newCleaningId
        })
        return reslut
    })
    return resluts

}))

/**
 * 获取入住信息
 */
router.get("/getCheckInRecord", asyncHandler(async (req, res) => {
    let keyword = {};
    if (req.query.keyword) {
        keyword = {
            dormitoryId: +req.query.keyword.dormitoryId || null,
            floorId: +req.query.keyword.floorId || null,
            roomNoId: +req.query.keyword.roomNoId || null,
            loginId: req.query.keyword.loginId || null,
            nickname: req.query.keyword.nickname || null,
        };
    }
    const reslut = await getCheckInRecord(+req.query.page || 1, +req.query.limit || 10, keyword)
    return reslut
}))
/**
 * 分配宿舍
 */
router.get("/getClass/:condition", asyncHandler(async (req, res) => {
    const reslut = await getClass(+req.params.condition);
    return reslut;
}))
/**
 * 获取未分配的宿舍
 */
router.get("/getUnallocated", asyncHandler(async (req, res) => {
    const reslut = await getUnallocated();
    return reslut
}))

/**
 * 分配宿舍
 */
router.post("/distributionRoom", asyncHandler(async (req, res) => {
    const resluts = await init.sequelize.transaction(async t => {
        if (typeof req.body.RoomArr === "string") {
            req.body.RoomArr = new Array(req.body.RoomArr)
        }
        const RoomArr = req.body.RoomArr.map(item => {
            return +item
        })

        const reslut = await distributionRoom(RoomArr, +req.body.classId)
        return reslut
    })
    return resluts
}))

module.exports = router